package com.shangze.test.app;

import android.app.Activity;
import android.content.Context;

import java.util.Stack;

/**
 * Created by yjm on 2017/10/16.
 * 初始化操作类
 */
public class Application extends android.app.Application {
    private static Stack<Activity> activityStack;
    private static Context context;
    private static Application instance;
    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();

    }
    public synchronized static Application getInstance() {
        if (null == instance) {
            instance = new Application();
        }
        return instance;
    }
    /**
     * 获取全局Context
     */
    public static Context getContext() {
        return context;
    }

    /**
     * 打开Activity
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 关闭Activity
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 关闭指定Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 关闭所有的Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 当前界面恢复时的操作
     */
    public void resumeActivity(Activity activity) {
        if (activityStack.lastElement() == activity) {
            return;
        }
        activityStack.remove(activity);
        activityStack.push(activity);
    }
}
